package com.zjw.zy.coreui.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.zjw.zy.R
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import kotlin.math.cos
import kotlin.math.sin

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/14 10:22
 */
class SunTimeView : View {

    private lateinit var mSunTimeData: TimeUtils.SunTimeBean
    private lateinit var mCurrentTime: TimeUtils.TimeBean
    private var init = false

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }


    fun bindData(sunTimeBean: TimeUtils.SunTimeBean, current: TimeUtils.TimeBean) {
        mSunTimeData = sunTimeBean
        mCurrentTime = current
        init = true
        invalidate()
    }


    override fun draw(canvas: Canvas?) {
        if (!init) {
            super.draw(canvas)
            return
        }
        //自定义view绘画
        canvas?.apply {
            val width = measuredWidth.toFloat()
            val height = measuredHeight.toFloat()
            val paddingWidth = width / 12
            val paddingTop = height / 12
            //太阳连线半径
            val sunRadio = if (width > 2 * height) {
                height / 2 - paddingTop
            } else {
                width / 2 - paddingWidth
            }
            val paint = Paint()
            paint.color = Color.BLUE

            //canvas.drawColor(ResourcesUtils.getColor(R.color.sun_time_view_background_color))
            //矩形框确定view边界
//            canvas.drawLine(0f, 0f, width, 0f, paint)
//            canvas.drawLine(width, 0f, width, height, paint)
//            canvas.drawLine(width, height, 0f, height, paint)
//            canvas.drawLine(0f, height, 0f, 0f, paint)

            //画一个半圆
            paint.color = ResourcesUtils.getColor(R.color.sun_time_view_sun_radio_color)
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 5f
            canvas.drawArc(
                paddingWidth,
                paddingTop,
                paddingWidth + sunRadio * 2,
                paddingTop + sunRadio * 2,
                0f,
                -180f,
                false,
                paint
            )
            //日暮线
            canvas.drawLine(0f, sunRadio + paddingTop, width, sunRadio + paddingTop, paint)

            paint.color = ResourcesUtils.getColor(R.color.sun_time_view_sun_reset_radio_color)
            //日落区域
            canvas.drawArc(
                -paddingWidth,
                sunRadio - 10,
                paddingWidth,
                sunRadio + 2 * paddingWidth - 10,
                0f,
                90f,
                false,
                paint
            )
            canvas.drawArc(
                width - paddingWidth,
                sunRadio - 10,
                width + paddingWidth,
                sunRadio + 2 * paddingWidth - 10,
                90f,
                90f,
                false,
                paint
            )


            val alpha = (Math.PI / ((mSunTimeData.sunResetHour - mSunTimeData.sunRaiseHour) * 60
                    + mSunTimeData.sunResetMinute - mSunTimeData.sunRaiseMinute
                    ) * ((mCurrentTime.hour - mSunTimeData.sunRaiseHour) * 60
                    + mCurrentTime.minute - mSunTimeData.sunRaiseMinute)).toFloat()

            //如果是白天
            if (alpha >= 0 && alpha <= Math.PI) {
                val stopX = sunRadio + paddingWidth - sunRadio * cos(alpha)
                val stopY = sunRadio + paddingTop - sunRadio * sin(alpha)


                //虚线
                paint.style = Paint.Style.STROKE
                paint.color = ResourcesUtils.getColor(R.color.sun_time_view_sun_radio_color)
                paint.pathEffect = DashPathEffect(FloatArray(2).apply {
                    this[0] = 5f
                    this[1] = 5f
                }, 0f)
                canvas.drawLine(
                    paddingWidth + sunRadio,
                    paddingTop + sunRadio,
                    stopX,
                    stopY,
                    paint
                )

                //在停住的地方画一个圆圈
                paint.color = ResourcesUtils.getColor(R.color.sun_time_view_sun_color)
                paint.style = Paint.Style.FILL
                canvas.drawCircle(stopX, stopY, sunRadio / 10, paint)
            } else {
                //简单画一个月亮

                //外矩宽度
                val outM = sunRadio / 2
                //内矩宽度
                val outLeft = paddingWidth + sunRadio - outM * 0.75
                val outRight = outLeft + outM

                val top = paddingTop + (sunRadio - outM) / 2
                val bottom = top + outM

                paint.color = ResourcesUtils.getColor(R.color.sun_time_view_sun_radio_color)
                paint.style = Paint.Style.FILL
                //外弧度
                canvas.drawArc(
                    outLeft.toFloat(),
                    top,
                    outRight.toFloat(),
                    bottom,
                    240f,
                    240f,
                    true,
                    paint
                )
            }

        }
        super.draw(canvas)
    }
}